﻿//---------------------------------------------------------------------
// Copyright (c) 2007 Digerati Technologies, LLC.  All Rights Reserved.
//
// Release under the Microsoft Public License (Ms-PL).
//---------------------------------------------------------------------

using System;
using System.IO;

namespace DigeratiTech.NDiagnostics
{
    /// <summary>
    /// This <see cref="DiagnosticTask"/> verifies that the specified .NET Framework is installed.
    /// </summary>
    public class NETFXTask : DiagnosticTask
    {
        #region Properties...

        /// <summary>
        /// Gets or sets the version of the .NET Framework to check for.
        /// </summary>
        /// <value>The version of the .NET Framework to check for.</value>
        public string Version { get; set; }

        #endregion

        #region Constructors...

        /// <summary>
        /// Initializes a new instance of the <see cref="NETFXTask"/> class.
        /// </summary>
        /// <param name="group">The <see cref="TaskGroup"/> this task belongs to.</param>
        /// <param name="name">The task name.</param>
        /// <param name="version">The version of the .NET Framework to check for.</param>
        public NETFXTask(TaskGroup group, string name, string version)
        {
            taskGroup = group;
            Name = name;
            Version = version;
            Status = TaskStatus.NotRun;
        }

        #endregion

        #region Methods...

        /// <summary>
        /// Executes the task.
        /// </summary>
        /// <returns>
        /// 	<c>true</c> if the task is successful; otherwise, <c>false</c>.
        /// </returns>
        public override bool Execute()
        {
            // Build the path where the specified .NET Framework should be installed
            string windowsFolder = Path.Combine(Path.Combine(Path.Combine(Environment.GetEnvironmentVariable("SystemRoot"), "Microsoft.NET"), "Framework"), "v" + Version);

            if (Directory.Exists(windowsFolder))
            {
                Status = TaskStatus.Completed;
                Message = string.Empty;
            }
            else
            {
                Status = TaskStatus.Error;
                Message = "The .NET Framework verion '" + Version + "' was not found.";
            }

            return (Message.Length == 0);
        }

        #endregion
    }
}
